import 'dart:ffi';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_colorpicker/flutter_colorpicker.dart';

Future<String?> showTextAlertDialog(
    {required BuildContext context,
    String? defaultValue,
    String? title,
    String? placeholder,
    String? okText,
    String? cancelText}) async {
  TextEditingController controller = TextEditingController();
  controller.text = defaultValue ?? "";
  final FocusNode focusNode = FocusNode();
  return await showDialog<String>(
      context: context,
      builder: (context) {
        WidgetsBinding.instance.addPostFrameCallback((_) {
          FocusScope.of(context).requestFocus(focusNode);
        });
        return CupertinoAlertDialog(
          title: title == null ? null : Text(title),
          content: Padding(
            padding: const EdgeInsets.all(5),
            child: CupertinoTextField(
              controller: controller,
              focusNode: focusNode,
              placeholder: placeholder,
            ),
          ),
          actions: <Widget>[
            TextButton(
              child: Text(cancelText ?? 'Cancel'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            TextButton(
              child: Text(okText ?? 'OK'),
              onPressed: () {
                Navigator.of(context).pop(controller.text);
              },
            ),
          ],
        );
      });
}

Future<Bool?> showTipsAlertDialog(
    {required BuildContext context,
    String? title,
    String? tips,
    String? okText,
    String? cancelText}) async {
  return await showDialog<Bool>(
      context: context,
      builder: (context) {
        return CupertinoAlertDialog(
          title: title == null ? null : Text(title),
          content: tips == null ? null : Text(tips),
          actions: <Widget>[
            TextButton(
              child: Text(cancelText ?? 'Cancel'),
              onPressed: () {
                Navigator.of(context).pop(false);
              },
            ),
            TextButton(
              child: Text(okText ?? 'OK'),
              onPressed: () {
                Navigator.of(context).pop(true);
              },
            ),
          ],
        );
      });
}

showColorPickerDialog(
    {required BuildContext context, String? title, required Color initColor}) {
  Color changeColor = initColor;
  return showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: title != null ? Text(title) : null,
          content: SingleChildScrollView(
              child: ColorPicker(
            pickerColor: initColor,
            labelTypes: const [],
            hexInputBar: true,
            onColorChanged: (Color color) {
              changeColor = color;
            },
          )),
          actions: [
            TextButton(
              child: Text("取消"),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            TextButton(
              child: Text("确定"),
              onPressed: () {
                Navigator.of(context).pop(changeColor);
              },
            ),
          ],
        );
      });
}
